package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.TbCity;
import com.travel.entity.TbHotel;
import com.travel.mapper.CityMapper;
import com.travel.mapper.HotelMapper;
import com.travel.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;

@Service
public class HotelServiceImpl extends ServiceImpl<HotelMapper, TbHotel> implements HotelService {

    @Autowired
    CityMapper cityMapper;

    @Override
    public ResultMsg getAllByCnameHaddr(String cityName, String hotelAddr) {
        // 先查城市
        QueryWrapper<TbCity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("city_name",cityName);
        TbCity city = cityMapper.selectOne(wrapper1);
        // 再查酒店
        QueryWrapper<TbHotel> wrapper2 = new QueryWrapper<>();
        if (cityName!=null && cityName.length()!=0){
            wrapper2.eq("city_id",city.getCityId());
        }
        if (hotelAddr!=null && cityName.length()!=0){
            wrapper2.like("hotel_addr",hotelAddr);
        }
        List<TbHotel> hotelList = super.list(wrapper2);
        if (hotelList != null || hotelList.size()!=0){
            return ResultMsg.success(hotelList);
        }else {
            return ResultMsg.error(300,"出错，未找到酒店！");
        }

    }

    @Override
    public ResultMsg adminInsertHotel(TbHotel hotel) {
        boolean insertHotel = super.save(hotel);
        if (insertHotel){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(303);
    }

    @Override
    public ResultMsg adminUpdateHotel(TbHotel hotel) {
        boolean updateHotel = super.updateById(hotel);
        if (updateHotel){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(303);
    }

    @Override
    public ResultMsg adminDeleteHotel(Integer hotelId) {
        boolean removeById = super.removeById(hotelId);
        if (removeById){
            return ResultMsg.success(200);
        }
        return ResultMsg.error(303);
    }

    @Override
    public ResultMsg getAllPage(Integer current, Integer size) {

        List<TbCity> cityList = cityMapper.selectList(null);
        HashMap<Integer, String> hashMap = new HashMap<>();
        for (TbCity tbCity : cityList) {
            hashMap.put(tbCity.getCityId(),tbCity.getCityName());
        }

        Page<TbHotel> hotelPage = new Page<>(current,size);
        Page<TbHotel> page = super.page(hotelPage);

        List<TbHotel> records = page.getRecords();
        for (TbHotel record : records) {
            record.setCityName(hashMap.get(record.getCityId()));
        }

        if (page!=null || page.getSize()!=0){
            return ResultMsg.success(page);
        }
        return ResultMsg.error(304);
    }

    @Override
    public ResultMsg getOne(Integer hotelId) {
        TbHotel hotel = super.getById(hotelId);
        if (hotel!=null){
            return ResultMsg.success(hotel);
        }
        return ResultMsg.error(304);
    }

    @Override
    public ResultMsg recommendHotel(Integer nums) {
        HashSet<Integer> set = new HashSet<>();
        while (true){
            set.add((int) (Math.random() * 10+1));
            if (set.size()==nums){
                break;
            }
        }
        QueryWrapper<TbHotel> wrapper = new QueryWrapper<>();
        wrapper.in("hotel_id",set);
        List<TbHotel> hotelList = super.list(wrapper);
        if (hotelList != null || hotelList.size()!=0){
            return ResultMsg.success(hotelList);
        }
        return ResultMsg.error(300,"出错，未找到酒店！");
    }

    @Override
    public ResultMsg uploadPic(MultipartFile pic) {
//        String filename = pic.getOriginalFilename();
        String name = UUID.randomUUID().toString();
        File file = new File("./target/classes/static/imgs");
        if (!file.exists())file.mkdirs();

        String path = "./target/classes/static/imgs/" + name + ".jpg";
        String pathBack = "http://localhost:8080/imgs/" + name + ".jpg";
        try {
            byte[] fileData = pic.getBytes();

            FileOutputStream fos = new FileOutputStream(path);
            fos.write(fileData);

        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.success(pathBack);
    }

    @Override
    public ResultMsg getHotelBetween(Integer min, Integer max) {
        List<TbHotel> hotelList = null;
        if (min != null && max != null){
            QueryWrapper<TbHotel> wrapper = new QueryWrapper<>();
            wrapper.between("hotel_price",min,max);
            hotelList = super.list(wrapper);
        }else if (min != null && max == null){
            QueryWrapper<TbHotel> wrapper = new QueryWrapper<>();
            wrapper.ge("hotel_price",min);
            hotelList = super.list(wrapper);
        }else if (min == null && max != null){
            QueryWrapper<TbHotel> wrapper = new QueryWrapper<>();
            wrapper.le("hotel_price",max);
            hotelList = super.list(wrapper);
        }else {
            hotelList = super.list();
        }

        if (hotelList != null || hotelList.size() != 0){
            return ResultMsg.success(hotelList);
        }else {
            return ResultMsg.error(300,"出错，未找到酒店！");
        }
    }


    @Override
    public ResultMsg getAll() {
        List<TbHotel> hotelList = super.list();
        if (hotelList != null || hotelList.size()!=0){
            return ResultMsg.success(hotelList);
        }
        return ResultMsg.error(300,"出错，未找到酒店！");
    }

    @Override
    public ResultMsg getAllByCityName(String cityName) {
        // 先查城市
        QueryWrapper<TbCity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("city_name",cityName);
        TbCity city = cityMapper.selectOne(wrapper1);
        // 再查酒店
        QueryWrapper<TbHotel> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("city_id",city.getCityId());
        List<TbHotel> hotelList = super.list(wrapper2);
        if (hotelList != null){
            return ResultMsg.success(hotelList);
        }else {
            return ResultMsg.error(300,"出错，未找到酒店！");
        }
    }

    @Override
    public ResultMsg getAllByHotelId(int hotelId) {
        TbHotel hotel = super.getById(hotelId);
        if (hotel != null){
            return ResultMsg.success(hotel);
        }else {
            return ResultMsg.error(300,"出错，未找到酒店！");
        }
    }
}
